package cn.etynit.web.controller;

import cn.etynit.domain.ExpenseElectricity;
import cn.etynit.dto.RechargeDto;
import cn.etynit.query.ExpenseElectricityQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import cn.etynit.service.IExpenseElectricityService;
import cn.etynit.vo.RechargeVo;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/expenseElectricity")
@Api(value = "业主电费接口",description = "此接口是业主电费相关的类")
public class ExpenseElectricityController {

    @Autowired
    public IExpenseElectricityService expenseElectricityService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody ExpenseElectricity expenseElectricity){
        if(expenseElectricity.getId()!=null){
            expenseElectricityService.updateById(expenseElectricity);
        }else{
            expenseElectricityService.insert(expenseElectricity);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        expenseElectricityService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(expenseElectricityService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(expenseElectricityService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ExpenseElectricityQuery query){
        Page<ExpenseElectricity> page = new Page<ExpenseElectricity>(query.getPage(),query.getRows());
        page = expenseElectricityService.selectPage(page);
        return JsonResult.success(new PageList<ExpenseElectricity>(page.getTotal(),page.getRecords()));
    }


    /**
     * 费用充值
     * @param rechargeVo
     * @return
     */
    @ApiOperation(value = "业主电费充值",notes = "业主电费充值",response = JsonResult.class)
    @RequestMapping(value="/recharge",method= RequestMethod.POST)
    public JsonResult recharge(@RequestBody RechargeVo rechargeVo){
        return JsonResult.success(expenseElectricityService.recharge(rechargeVo));
    }

    /**
     * 根据userid查询账单
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询业主电费账单及余额",notes = "查询业主电费账单及余额",response = JsonResult.class)
    @RequestMapping(value = "/selectByUserId/{userId}",method = RequestMethod.GET)
    public JsonResult selectByUserId(@PathVariable("userId")Long userId){
        return JsonResult.success(expenseElectricityService.selectByUserId(userId));
    }
}
